/**
 * <b>项目名：</b>hclv-web<br/>  
 * <b>包名：</b>com.flea.hclv.service.impl<br/>  
 * <b>文件名：</b>HVisitorRebackPaymentRecoderServiceImpl.java<br/>  
 * <b>版本信息：</b>1.0<br/>  
 * <b>日期：</b>2018年11月15日 下午11:10:46<br/>  
 * <b>COPYRIGHT 2010-2012 ALL RIGHTS RESERVED 虫子科技</b>-版权所有<br/>
 */
package com.flea.hclv.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.HVisitorOrderMapper;
import com.flea.hclv.model.entity.HVisitorOrder;
import com.flea.system.base.util.StringUtil;
import com.flea.system.utils.FleaStringUtils;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.flea.hclv.util.ServerResponse;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.flea.hclv.model.dao.HVisitorRebackPaymentRecoderMapper;
import com.flea.hclv.model.entity.HVisitorRebackPaymentRecoder;
import com.flea.hclv.service.HVisitorRebackPaymentRecoderService;
import com.flea.system.bean.FleaPage;
import com.flea.system.platform.session.FleaOrgUtils;

/**
 * @description
 * @createTime 2018年11月15日 下午11:10:46
 * @modifyTime 
 * @author myflea@163.com
 * @version 1.0
 */
@Service
public class HVisitorRebackPaymentRecoderServiceImpl implements HVisitorRebackPaymentRecoderService {
	@Autowired
	private HVisitorRebackPaymentRecoderMapper visitorRebackPaymentRecoderMapper;
	@Autowired
	private HVisitorOrderMapper hVisitorOrderMapper;

	@Override
	public ServerResponse<FleaPage<HVisitorRebackPaymentRecoder>> getRebackRecoderByOrgIdForPaging(HttpServletRequest request,
																								   int curPage, int pageSize, String orderNo) {
		RowBounds rowBounds = new RowBounds(curPage, pageSize);
		String orgId = FleaOrgUtils.getCurrentUserDeptUuid(request);
		List<HVisitorRebackPaymentRecoder> list = visitorRebackPaymentRecoderMapper.findByOrgIdForPaging(rowBounds,orgId,orderNo);
		FleaPage<HVisitorRebackPaymentRecoder> result = new FleaPage<HVisitorRebackPaymentRecoder>(list);
		return ServerResponse.createBySuccess(result);
	}

  @Override
  public ServerResponse addRebackRecoder(String model, HttpServletRequest request)
      throws Exception {
	  //数据判断,这个订单是否已经完成或者取消，只有取消的订单才可以添加退款记录
    if(StringUtil.isEmpty(model)){
        return ServerResponse.createByErrorMessage("参数为空");
    }
    JSONObject jsb = JSONObject.parseObject(model);
    if(null==jsb){
      return ServerResponse.createByErrorMessage("参数转版异常");
    }
    String orderNo = jsb.getString("orderNo");
    if(StringUtil.isEmpty(orderNo)){
      return ServerResponse.createByErrorMessage("orderNo参数为空");
    }
    HVisitorOrder orderInfo = hVisitorOrderMapper.selectByOrderNo(orderNo);
    if(null==orderInfo){
      return ServerResponse.createByErrorMessage("该订单已经不存在");
    }
    if(!HConstant.D_ORDER_STATUS_CANCEL.equals(orderInfo.getStatus())){
      return ServerResponse.createByErrorMessage("该订单不为取消状态，不可进行退款");
    }
    String entitys = jsb.getString("rebackRecoder");
    if(StringUtil.isEmpty(entitys)){
      return ServerResponse.createByErrorMessage("退款参数为空");
    }
    HVisitorRebackPaymentRecoder entity = JSONObject.parseObject(entitys,HVisitorRebackPaymentRecoder.class);
    if(null==entity){
      return ServerResponse.createByErrorMessage("退款参数转版异常");
    }
    String userId = FleaOrgUtils.getCurUserUuid(request);

    entity.setId(FleaStringUtils.getGenerateUUID());
    entity.setCreateUser(userId);
    entity.setOrderNo(orderNo);

    boolean result = visitorRebackPaymentRecoderMapper.insertSelective(entity)==1;
    if(!result){
      throw new Exception("退款失败，请联系运维人员");
    }

    return ServerResponse.createBySuccessMessage("退款操作成功");
  }

  @Override
  public ServerResponse findRebackRecoderByOrderNo(String orderNo, HttpServletRequest request,int curPage,int pageSize)
      throws Exception {
    RowBounds rowBounds = new RowBounds(curPage, pageSize);
    List<HVisitorRebackPaymentRecoder> result = visitorRebackPaymentRecoderMapper.findByOrderNo(orderNo,rowBounds);

    FleaPage<HVisitorRebackPaymentRecoder> pageReuslt = new FleaPage<>(result);
    pageReuslt.setCurPageData(coverCoderToName(result));
    return ServerResponse.createBySuccess(pageReuslt);
  }

  @Override
  public List<HVisitorRebackPaymentRecoder> findRebackbyOrderNos(List<String> orderNos) {
	  if (null==orderNos||orderNos.size()==0){
	    return new ArrayList<>();
    }
    List<HVisitorRebackPaymentRecoder> result = visitorRebackPaymentRecoderMapper.findByOrderNos(orderNos);
    return result;
  }

  private List<HVisitorRebackPaymentRecoder> coverCoderToName(List<HVisitorRebackPaymentRecoder> result){
	  List<HVisitorRebackPaymentRecoder> newResult = new ArrayList<>();
    for (HVisitorRebackPaymentRecoder item:result
    ) {
      item.setCreateUserName(FleaOrgUtils.getUserByUserUuid(item.getCreateUser()).getUserName());

      newResult.add(item);
    }
	  return newResult;
  }

}
